package intro.databases.gtCarRental.ui;

import intro.databases.gtCarRental.GTCRActivity;
import intro.databases.gtCarRental.R;
import intro.databases.gtCarRental.ui.dialog.CarAvailabilityDialog;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CarAvailability extends GTCRActivity {
	LinearLayout table;
	
	@Override
	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.car_availability);

		Intent x = getIntent();
		final ArrayList<String> carSearchValues = x.getStringArrayListExtra("carSearchValues");
		ResultSet rS = dbConn.carSearch(carSearchValues);
		table = (LinearLayout) findViewById(R.id.linearLayout_carAvailability_table);
		
		try {
			while(rS.next()) {
				View v = getLayoutInflater().inflate(R.layout.car_availability_table, null);
				TextView carModel = (TextView) v.findViewById(R.id.textView_carAvailability_carModel);
				TextView estCost = (TextView) v.findViewById(R.id.textView_carAvailability_estimatedCost);
				carModel.setText(rS.getString(1));
				estCost.setText(rS.getInt(12) + "");
				
				final String carM = rS.getString(1);
				final String carT = rS.getString(2);
				final String loc = rS.getString(3);
				final String col = rS.getString(4);
				final String hourR = rS.getString(5);
				final String dailyR = rS.getString(6);
				final String seatingC = rS.getString(10);
				final String transT = rS.getString(9);
				final String btConn = (rS.getString(8).equals("1")) ? "Yes" : "No";
				final String auxC = (rS.getString(7).equals("1")) ? "Yes" : "No";
				final String estC = rS.getString(12);	
				final String vehSno = rS.getString(13);

				Button details = (Button) v.findViewById(R.id.button_carAvailability_details);
				details.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						ArrayList<String> carDetailValues = new ArrayList<String>();
						carDetailValues.add(carM);
						carDetailValues.add(carT);
						carDetailValues.add(loc);
						carDetailValues.add(col);
						carDetailValues.add(hourR);
						carDetailValues.add(dailyR);
						carDetailValues.add(seatingC);
						carDetailValues.add(transT);
						carDetailValues.add(btConn);
						carDetailValues.add(auxC);
						carDetailValues.add(estC);
						
						Intent intent = new Intent(CarAvailability.this, CarAvailabilityDialog.class);
						intent.putStringArrayListExtra("carDetailValues", carDetailValues);
						startActivity(intent);
					}
				});
				
				Button register = (Button) v.findViewById(R.id.button_carAvailability_register);
				register.setOnClickListener(new View.OnClickListener() {					
					@Override
					public void onClick(View v) {
						dbConn.createReservation(application.currentUser.username, (carSearchValues.get(0) + " " + carSearchValues.get(1)), (carSearchValues.get(2) + " " + carSearchValues.get(3)), estC, loc, vehSno);
						Toast.makeText(CarAvailability.this, "Reservation made", Toast.LENGTH_LONG).show();
						CarAvailability.this.finish();
					}
				});
				
				table.addView(v);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
